package com.usst.handle;


import org.springframework.stereotype.Controller;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.mysql.jdbc.StringUtils;
import com.usst.entity.Comment;
import com.usst.entity.User;
import com.usst.service.CommentService;
import com.usst.service.UserService;


@Controller
public class UserHandler extends DefaultHandler {

	private CommentService commentService;
	
	private UserService userService;
	
	private User user;
	
	public UserHandler() {
		super();
	}
	
	public UserHandler(CommentService commentService, UserService userService) {
		this();
		this.commentService = commentService;
		this.userService = userService;
	}
    
    @Override
    public void startDocument() throws SAXException {
    	
    }
    
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
    
    }
    
	@Override
    public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
        if ("row".equals(name)) {
        	
            user= new User();
            user.setUserId(Long.valueOf(attributes.getValue("Id")));
            
            if (!StringUtils.isNullOrEmpty(attributes.getValue("Reputation"))) {
            	user.setReputation(Long.valueOf(attributes.getValue("Reputation")));
            }
            
            if (!StringUtils.isNullOrEmpty(attributes.getValue("CreationDate"))) {
            	user.setCreationDate(attributes.getValue("CreationDate"));
            }
            
            if (!StringUtils.isNullOrEmpty(attributes.getValue("DisplayName"))) {
            	user.setDisplayName(attributes.getValue("DisplayName"));
            }
            
            if (!StringUtils.isNullOrEmpty(attributes.getValue("LastAccessDate"))) {
            	user.setLastAccessDate(attributes.getValue("LastAccessDate"));
            }
            
            if (!StringUtils.isNullOrEmpty(attributes.getValue("WebsiteUrl"))) {
            	user.setWebsiteUrl(attributes.getValue("WebsiteUrl"));
            }
            
            if (!StringUtils.isNullOrEmpty(attributes.getValue("Location"))) {
            	user.setLocation(attributes.getValue("Location"));
            }
            
            if (!StringUtils.isNullOrEmpty(attributes.getValue("AboutMe"))) {
            	user.setAboutMe(attributes.getValue("AboutMe"));
            }
            
            if (!StringUtils.isNullOrEmpty(attributes.getValue("Views"))) {
            	user.setViews(Long.valueOf(attributes.getValue("Views")));
            }
            
            if (!StringUtils.isNullOrEmpty(attributes.getValue("UpVotes"))) {
            	user.setUpVotes(Long.valueOf(attributes.getValue("UpVotes")));
            }
            
            if (!StringUtils.isNullOrEmpty(attributes.getValue("DownVotes"))) {
            	user.setDownVotes(Long.valueOf(attributes.getValue("DownVotes")));
            }
            
            if (!StringUtils.isNullOrEmpty(attributes.getValue("Age"))) {
            	user.setAge(Long.valueOf(attributes.getValue("Age")));
            }
            
            if (!StringUtils.isNullOrEmpty(attributes.getValue("AccountId"))) {
            	user.setAccountId(Long.valueOf(attributes.getValue("AccountId")));
            }
            
        }
    }
    
    @Override
    public void endElement(String uri, String localName, String name) throws SAXException {
        if (user != null && "row".equals(name)) {
        	Comment comment = commentService.findOneByUserId(user.getUserId());
        	if (comment != null) {
        		userService.save(user);
        	}
        	comment = null;
        	user = null;
        }
    }
    
}
